package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dao.DishCategoryMapper;
import org.example.dao.InventoryMapper;
import org.example.dish_pojo.*;
import org.example.service.IDishManageService;
import org.example.dish_pojo.vo.DishManageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品基础信息表 前端控制器
 * </p>
 *
 * @author mp
 * @since 2025-08-04
 */
@RestController
@RequestMapping("/dishManagement")
public class DishManageController {

    @Autowired
    private IDishManageService dishInfoService;
    @Autowired
    private DishCategoryMapper dishCategoryMapper;
    @Autowired
    private InventoryMapper inventoryMapper;


    // 分页查询菜品
    @RequestMapping("/listDishManagement")
    public Result listDishInfo(@RequestBody DishManageQuery dishManageQuery) {
        Page<DishInfo> page = new Page<>(dishManageQuery.getCurrent(), dishManageQuery.getSize());
        QueryWrapper<DishInfo> wrapper = new QueryWrapper<>();

        Integer code = dishManageQuery.getCode();
        if (code != null && code != 0) {
            wrapper.eq("code", code);
        }

        dishInfoService.page(page, wrapper);
        List<DishInfo> list = page.getRecords();

        if (list.size() > 0) {
            for (DishInfo d : list) {
                Integer categoryId = d.getCategoryId();
                Integer dishId = d.getDishId();
                DishCategory dishCategory = dishCategoryMapper.selectById(categoryId);

                // 修改查询 Inventory 的逻辑
                QueryWrapper<Inventory> inventoryWrapper = new QueryWrapper<>();
                inventoryWrapper.eq("dish_id", dishId);
                Inventory inventory = inventoryMapper.selectOne(inventoryWrapper);

                d.setCname(dishCategory.getCategoryName());
                d.setCurrentStock(inventory.getCurrentStock());
                d.setUname(inventory.getDishdanwei());
            }
        }
        return new Result(200, "查询成功", page);
    }


    // 停售菜品 状态字段isOnSale
    @RequestMapping("/stopDish")
    public Result stopDish(@RequestBody DishInfo dishInfo) {
        DishInfo dishInfo1 = new DishInfo();
        dishInfo1.setDishId(dishInfo.getDishId());
        dishInfo1.setIsOnSale(false);
        boolean update = dishInfoService.updateById(dishInfo1);
        if (update) {
            return new Result(200, "停售成功", null);
        }
        return new Result(500, "停售失败", null);
    }


    // 开售菜品 状态字段isOnSale
    @RequestMapping("/startDish")
    public Result startDish(@RequestBody DishInfo dishInfo) {
        DishInfo dishInfo1 = new DishInfo();
        dishInfo1.setDishId(dishInfo.getDishId());
        dishInfo1.setIsOnSale(true);
        boolean update = dishInfoService.updateById(dishInfo1);
        if (update) {
            return new Result(200, "开售成功", null);
        }
        return new Result(500, "开售失败", null);
    }


}

